package FSPackage;

import jade.core.Agent;
import jade.core.behaviours.*;
import jade.lang.acl.*;
import java.util.*;

class app {
    String name;    // Application name
    int bw;         // required bandwidth

    public app(String appName, int BW) {
        name = appName;
        bw = BW;
    }
}

public class FSAgent extends Agent
{
    private Vector<app> apps = new Vector<app>();
    private Random r = new Random();

    public FSAgent() {
        // Application database is made
        // with initial apps
        apps.add(new app("SW1", 10));
        apps.add(new app("SW2", 15));
        apps.add(new app("MSWORD", 35));
        apps.add(new app("NetBeans", 30));
    }

    protected void setup()
    {
        addBehaviour(new CyclicBehaviour(this)
        {
            public void action() {
                ACLMessage req = receive();

                if(req != null) {
                    int avBW = r.nextInt(300); // available bandwidth set to 0
                    String reply = null;

                    System.err.println("Fileserver started with bw: " + avBW);
                    
                    ACLMessage replyMSG = req.createReply();
                    replyMSG.setPerformative( ACLMessage.INFORM);

                    for (int i = 0; i < apps.size(); i++) {
                        if(req.getContent().equals(apps.elementAt(i).name))
                            avBW = apps.elementAt(i).bw;
                    }

                    reply = "" + avBW;

                    replyMSG.setContent(reply);
                    send(replyMSG);
                }

                block();
            }
        }
        );
    }
}